package com.service.impl;

import com.dao.TeacherDao;
import com.dto.in.AddTeacherDto;
import com.entity.Subject;
import com.entity.SubjectInfo;
import com.entity.Teacher;
import com.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018/10/16.
 */
@Service
public class TeacherServerImpl implements TeacherService {
    @Autowired
    private TeacherDao teacherDao;

    @Override
    public List<Teacher> choseTeacher(Integer count, Integer cityId, Integer subjectId, Integer areaId) {
        List<Teacher> list = teacherDao.choseTeacher(count, cityId, subjectId, areaId);
            List<Teacher> newList = new ArrayList<>();
            for (int j = 0; j < list.size(); j++) {
                StringBuffer strb = new StringBuffer();
                String[] sub = list.get(j).getSubjectId().split(",");
                int a = 0;
                for (int i = 0; i < sub.length; i++) {
                    String res = teacherDao.findSubjectName(sub[i]);
                    strb.append(res + ",");
                    if (subjectId != 0) {
                        if (Integer.parseInt(sub[i]) == subjectId) {
                            a = a + 1;
                        }
                    }
                }
                strb.delete(strb.length() - 1, strb.length());
                list.get(j).setSubjectName(strb.toString());
                if (a > 0) {
                    newList.add(list.get(j));
                }
            }
            if (subjectId.equals(0)){
                return list;
            }else {
                return newList;
            }


    }

    @Override
    public int addTeacher(AddTeacherDto addTeacherDto) {
        int success = teacherDao.addTeacher(addTeacherDto);
        return success;
    }

    @Override
    public Teacher selectTeacher(String userId) {
        Teacher teacher = teacherDao.selectTeacher(Integer.parseInt(userId));
        StringBuffer strb = new StringBuffer();
        if (teacher.getSubjectId() != null) {
            String[] sub = teacher.getSubjectId().split(",");
            for (int i = 0; i < sub.length; i++) {
                String res = teacherDao.findSubjectName(sub[i]);
                strb.append(res + ",");
            }
            strb.delete(strb.length() - 1, strb.length());
            teacher.setSubjectName(strb.toString());
        }
        return teacher;
    }

    @Override
    public int updateResumeStatus(Integer userId, Boolean checked) {
        if (checked == true) {
            return teacherDao.updateResumeStatusNO(userId);
        } else {
            return teacherDao.updateResumeStatusYES(userId);
        }
//        String status = teacherDao.findResumeStatus(userId);
//        if(status.equals("YES")){
//
//        }else{
//
//        }
    }

    @Override
    public int findTeacherValue(Integer userId) {
        return teacherDao.findTeacherValue(userId);
    }
}
